package sort;

import java.util.Arrays;

public class SelectSort {   // 选择排序
    public static void main(String[] args) {
        int[] array = {68,27,15,19,28,68,6};
        selectSort2(array);
        System.out.println(Arrays.toString(array));
    }
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length - 1; i++){
            int index = i;
            for (int j = i + 1; j < array.length; j++) {
                if(array[j] < array[index]){
                    index = j;
                }
            }
            Swap.swap(array,i,index);
        }
    }
    public static void selectSort2(int[] array){  // 从两边同时进行
        int left = 0;
        int right = array.length - 1;
        while (left < right){
            int minIndex = left;
            int maxIndex = left;  // 改为right 是否可以省略后面的if(maxIndex == left)语句
            for (int i = left + 1; i <= right; i++) {
                if(array[i] < array[minIndex]){
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]){
                    maxIndex = i;
                }
            }
            Swap.swap(array, left, minIndex);
            if(maxIndex == left){
                maxIndex = minIndex;
            }
            Swap.swap(array, right, maxIndex);
            left++;
            right--;
        }
    }
}
